<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="终端编号" prop="clientNo">
        <el-input
          v-model="queryParams.clientNo"
          placeholder="请输入终端编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['device:gasEventOrigin:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="gasEventOriginList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="数据编号" align="center" prop="id" />
      <el-table-column label="终端编号" align="center" prop="clientNo" />
<!--      <el-table-column label="事件ID" align="center" prop="eventId" />-->
      <el-table-column label="原始ID" align="center" prop="originEventId" />
<!--      <el-table-column label="数据" align="center" prop="data" />-->
      <el-table-column label="16进制源数据" align="center" prop="hexData">
        <template slot-scope="scope" s>
          {{ substrData(scope.row.hexData) }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDataView(scope.row)"
            v-hasPermi="['device:gasEventOrigin:edit']"
          >查看波形</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改事件源数据对话框 -->
    <el-dialog title="事件波形查看" :visible.sync="open" width="40%" style="height: 45%" center append-to-body>
      <div id="dataViewDiv" style="height: 90%; width: 100%;"></div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import echarts from 'echarts'
import { listGasEventOrigin } from "@/api/device/gasEventOrigin";

export default {
  name: "GasEventOrigin",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 事件源数据表格数据
      gasEventOriginList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clientNo: null,
        eventId: null,
        originEventId: null,
        data: null,
        hexData: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      dataViewChartOption: null,
      dataViewChart: null
    };
  },
  created() {
    this.getList();
  },
  methods: {
    substrData(msg){
      if(msg && msg.length > 25){
        return msg.substring(0, 25) + '...'
      }
      return msg
    },
    /** 查询事件源数据列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listGasEventOrigin(this.queryParams).then(response => {
        this.gasEventOriginList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 按钮操作 */
    handleDataView(row) {
      this.open = true
      if(this.dataViewChart == null){
        this.dataViewChart = echarts.init(document.getElementById('dataViewDiv'));
      }
      this.drawDataViewChart(row)
    },
    drawDataViewChart(row){
      let _this = this
      let data = row.data
      let categories = [];
      for (let i = 0; i < row.data.length; i++) {
        categories.push(i+1);
      }
      _this.dataViewChartOption = {
        backgroundColor: '#333645',
        title: {
          text: '音频图',
          left: 'center'
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          left: '3%',
          data: ['x']
        },
        xAxis: {
          type: 'category',
          name: 'x',
          splitLine: {
            lineStyle:{
              type:'solid',
              color: '#fff'
            }
          },
          "axisLine": {
            lineStyle: {
              color: '#fff'
            }
          },
          axisLabel: {
            // interval: 0
          },
          data: categories
        },
        grid: {
          left: '3%',
          right: '4%',
          top: '10%',
          bottom: '4%',
          containLabel: true
        },
        yAxis: {
          name: 'y',
          splitLine: {
            lineStyle: {
              color: '#fff'
            }
          },

          axisLabel: {
            textStyle: {
              color: '#fff'
            }
          },

        },
        series: [{
          name: 'x',
          type: 'line',
          smooth: 1,
          symbol: 'none',
          lineStyle: {
            normal: {
              width: 2
            }
          },
          data: data
        }
        ],
        animation: false
      };
      _this.dataViewChart.setOption(_this.dataViewChartOption)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('device/gasEventOrigin/export', {
        ...this.queryParams
      }, `GasEventOrigin_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
